home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / µSim 1.0b5 folder / source / myMemory.c < prev    next >
Encoding:
Text File  |  1994-09-01  |  5.4 KB  |  185 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Globals.h"
  14. #include    "Animation.h"
  15. #include    "Disasm.h"
  16. #include    "Dump.h"
  17. #include    "myMemory.h"
  18. #include    "Registers.h"
  19. #include    "SimUtils.h"
  20.  
  21. #if defined(FabSystem7orlater)
  22.  
  23. #pragma segment Rare
  24.  
  25. /* myOpenFile: opens a dump file */
  26.  
  27. OSErr    myOpenFile(FSSpec *theFile, Ptr where, Size howmuch)
  28. {
  29. ParamBlockRec    myPB;
  30. EventRecord    dummyEv;
  31. short    myFileRefN;
  32. register OSErr    err;
  33.  
  34. SetCursor(*gWatchHandle);
  35. if ((err = FSpOpenDF(theFile, fsRdPerm, &myFileRefN)) == noErr) {
  36.     myPB.ioParam.ioCompletion = nil;
  37.     myPB.ioParam.ioRefNum = myFileRefN;
  38.     myPB.ioParam.ioBuffer = where;
  39.     myPB.ioParam.ioReqCount = howmuch;
  40.     myPB.ioParam.ioPosMode = fsFromStart;
  41.     myPB.ioParam.ioPosOffset = 0L;
  42.     (void)PBReadAsync(&myPB);
  43.     while (myPB.ioParam.ioResult > 0) {
  44.         SystemTask();
  45.         (void)EventAvail(everyEvent, &dummyEv);
  46.         }
  47.     err = myPB.ioParam.ioResult;
  48.     (void)FSClose(myFileRefN);
  49.     if (where == (Ptr)&gRegs)
  50.         ChangedAllRegisters();
  51.     else {
  52.         InvalDump();
  53.         InvalDisasm();
  54.         }
  55.     }
  56. SetCursor(&qd.arrow);
  57. return(err);
  58. }
  59.  
  60. /* mySaveFile: saves a dump file to disk */
  61.  
  62. OSErr mySaveFile(Ptr where, Size howmuch, OSType fType, short prompt, short defName)
  63. {
  64. StandardFileReply    mySFR;
  65. Str255    tempFName;
  66. ParamBlockRec    myPB;
  67. EventRecord    dummyEv;
  68. FSSpec    thisFSSpec;
  69. long    thisDirID;
  70. short    thisVRefNum, tmpFRefN;
  71. register SignedByte    state1, state2;
  72. register Handle    sH1 = (Handle)GetString(prompt);
  73. register Handle    sH2 = (Handle)GetString(defName);
  74. register OSErr    err;
  75.  
  76. err = noErr;
  77. state1 = WantThisHandleSafe(sH1);
  78. state2 = WantThisHandleSafe(sH2);
  79. StandardPutFile((ConstStr255Param)*sH1, (ConstStr255Param)*sH2, &mySFR);
  80. HSetState(sH2, state2);
  81. HSetState(sH1, state1);
  82. if (mySFR.sfGood) {
  83.     SetCursor(*gWatchHandle);
  84.     myPB.ioParam.ioCompletion = nil;
  85.     myPB.ioParam.ioBuffer = where;
  86.     myPB.ioParam.ioReqCount = howmuch;
  87.     myPB.ioParam.ioPosMode = fsFromStart;
  88.     if (mySFR.sfReplacing)
  89.         if (0)    /* test if file locked (FSpExchange does not) */
  90.             err = fLckdErr;
  91.         else {
  92.             MyNumToString(TickCount(), tempFName);
  93.             if ((err = FindFolder(mySFR.sfFile.vRefNum, kTemporaryFolderType,
  94.                                 kCreateFolder, &thisVRefNum, &thisDirID)) == noErr) {
  95.                 (void)FSMakeFSSpec(thisVRefNum, thisDirID, (ConstStr255Param)&tempFName, &thisFSSpec);
  96.                 if ((err = FSpCreate(&thisFSSpec, kFCR_MINE, fType,
  97.                                     mySFR.sfScript)) == noErr)
  98.                     if ((err = FSpOpenDF(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  99.                         myPB.ioParam.ioRefNum = tmpFRefN;
  100.                         myPB.ioParam.ioPosOffset = 0L;
  101.                         (void)PBWriteAsync(&myPB);    // in temporary file
  102.                         while (myPB.ioParam.ioResult > 0) {
  103.                             SystemTask();
  104.                             (void)EventAvail(everyEvent, &dummyEv);
  105.                             }
  106.                         (void)FSClose(tmpFRefN);
  107.                         if ((err = myPB.ioParam.ioResult) == noErr) {
  108.                             if ((err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE,
  109.                                                     fType, kSTR_ApplicationName, mySFR.sfScript))
  110.                                     == noErr)
  111.                                 if ((err = FSpExchangeFiles(&thisFSSpec, &(mySFR.sfFile)))
  112.                                         == noErr)
  113.                                     err = FSpDelete(&thisFSSpec);
  114.                             }
  115.                         else {
  116.                             (void)FSpDelete(&thisFSSpec);    // the temporary one
  117.                             if (err == dskFulErr) {
  118.                                 InitCursor();
  119.                                 if (CautionAlert_UPP(kALRT_NOSAFESAVE, myStdFilterProc) == ok) {
  120.                                     if ((err = FSpOpenDF(&(mySFR.sfFile), fsWrPerm,
  121.                                                         &tmpFRefN)) == noErr) {
  122.                                         myPB.ioParam.ioRefNum = tmpFRefN;
  123.                                         myPB.ioParam.ioPosOffset = 0L;
  124.                                         (void)PBWriteAsync(&myPB);
  125.                                         while (myPB.ioParam.ioResult > 0) {
  126.                                             SystemTask();
  127.                                             (void)EventAvail(everyEvent, &dummyEv);
  128.                                             }
  129.                                         (void)FSClose(tmpFRefN);
  130.                                         err = myPB.ioParam.ioResult;
  131.                                         }
  132.                                     }
  133.                                 else err = noErr; /* user clicked cancel in alert */
  134.                                 }
  135.                             }
  136.                         }
  137.                 }
  138.             }
  139. /* not replacing an existing file */
  140.     else if ((err = FSpCreate(&(mySFR.sfFile), kFCR_MINE, fType, mySFR.sfScript)) == noErr)
  141.         if ((err = FSpOpenDF(&(mySFR.sfFile), fsWrPerm, &tmpFRefN)) == noErr) {
  142.             myPB.ioParam.ioRefNum = tmpFRefN;
  143.             myPB.ioParam.ioPosOffset = 0L;
  144.             (void)PBWriteAsync(&myPB);
  145.             while (myPB.ioParam.ioResult > 0) {
  146.                 SystemTask();
  147.                 (void)EventAvail(everyEvent, &dummyEv);
  148.                 }
  149.             (void)FSClose(tmpFRefN);
  150.             if ((err = myPB.ioParam.ioResult) == noErr)
  151.                 err = AddSTRRes2Doc(&(mySFR.sfFile), kFCR_MINE, fType,
  152.                                     kSTR_ApplicationName, mySFR.sfScript);
  153.             }
  154.     SetCursor(&qd.arrow);
  155.     }
  156. return err;
  157. }
  158.  
  159.  
  160. OSErr    OpenProcessorState(FSSpecPtr theFile)
  161. {
  162. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  163. register OSErr    err;
  164.  
  165. if (noErr == (err = myOpenFile(theFile, (Ptr)&state, sizeof(state)))) {
  166.     BlockMoveData((Ptr)&state, (Ptr)&gParts, sizeof(gParts));
  167.     BlockMoveData((Ptr)(state + kNUMOFPARTS), (Ptr)&gRegs, sizeof(state) - sizeof(gParts));
  168.     ChangedAllRegisters();
  169.     ChangedAllBoxes();
  170.     }
  171. return err;
  172. }
  173.  
  174. OSErr    SaveProcessorState(void)
  175. {
  176. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  177.  
  178. BlockMoveData((Ptr)&gParts, (Ptr)&state, sizeof(gParts));
  179. BlockMoveData((Ptr)&gRegs, (Ptr)(state + kNUMOFPARTS), sizeof(state) - sizeof(gParts));
  180. return mySaveFile((Ptr)&state, sizeof(state), kFTY_REG, kSTR_REGSPROMPT, kSTR_REGSDEFNAME);
  181. }
  182.  
  183. #endif
  184.  
  185.